<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:doc="livcos.org/ns/doc"
  xmlns:c="livcos.org/ns/cosmos"
  xmlns:m="livcos.org/ns/map"
  xmlns:f="livcos.org/ns/xslt/function"
  xmlns="http://www.w3.org/1999/xhtml"
  exclude-result-prefixes="doc c f m">
  
  <xsl:import href="../../web/style/doc/document.xsl" />
  <xsl:import href="../../web/style/base.xsl" />
  <xsl:import href="../../web/style/web.xsl" />
  
  <xsl:param name="map" select="'../data/WebMap'"/>
  
  <xsl:variable name="history" select="doc(concat($map,'.history'))/*"/>
  
  <xsl:template match="/">
    <xsl:choose>
      <xsl:when test="doc-available($map)">
        <xsl:apply-templates select="document($map)/*" mode="run"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:call-template name="not_found">
          <xsl:with-param name="msg">Cannot find map '<xsl:value-of
            select="$map"/>'!</xsl:with-param>
        </xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  
  <xsl:template name="not_found">
    <xsl:param name="msg"/>
    <html>
      <head>
        <title>Not Found!</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <link href="{$toRootPath}/livcos.org/web/css/styles.css" rel="stylesheet" type="text/css"/>
      </head>
      <body>
        <p><xsl:value-of select="$msg"/></p>
      </body>
    </html>
  </xsl:template>
  
  <xsl:template match="m:map" mode="run">
    <html>
      <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <link href="{$toRootPath}/livcos.org/web/css/styles.css" rel="stylesheet" type="text/css"/>
        <link href="{$toRootPath}/livcos.org/web/ext/css/ext-all.css" rel="stylesheet" type="text/css"/>
        <script type="text/javascript" src="{$toRootPath}/livcos.org/web/ext/script/ext/ext-base.js"><!-- --></script>
        <script type="text/javascript" src="{$toRootPath}/livcos.org/web/ext/script/ext/ext-all-debug.js"><!-- --></script>
        <script type="text/javascript" src="script/grid.js"><!-- --></script>
        <script>
          window._data = {
            title: "Map <xsl:value-of select="$map"/>",
            columns: [
              {header: "ID", width: 60, sortable: true, type: 'string'},
              {header: "Label", width: 200, sortable: true, type: 'string'},
              {header: "Link", width: 320, sortable: true, renderer: renderLink},
              {header: "Description", width: 480, sortable: true, type: 'string'},
              {header: "Rels", width: 40, sortable: true, align: "right", type: 'int'},
              {header: "Chngs", width: 40, sortable: true, align: "right", type: 'int'},
              {header: "Last Chng", width: 66, sortable: true, type: 'string'},
              {header: "Author", width: 80, sortable: true, type: 'string'},
              ],
            content: [
              <xsl:apply-templates select="m:body/m:item"/>
            ]
          };
        </script>
      </head>
      <body>
        <div id="mapGrid"/>
      </body>
    </html>
  </xsl:template>
  
  <xsl:template match="m:item">
    <xsl:variable name="ids" select="@id | .//@id"/>
    <xsl:variable name="changes"
    	select="$history/*[(count(distinct-values(.//@ref | $ids)) - count(distinct-values(.//@ref))) != count($ids)]"/>
    <xsl:if test="position() > 1">,</xsl:if>
    <xsl:text>['</xsl:text>
    <xsl:value-of select="@id"/>
    <xsl:text>','</xsl:text>
    <xsl:value-of select="m:label"/>
    <xsl:text>','</xsl:text>
  	<xsl:value-of select="replace(doc:link[1]/@ref, '''', '\\''')"/>|<xsl:apply-templates select="doc:link[1]"/>
    <xsl:text>','</xsl:text>
    <xsl:apply-templates select="doc:paragraph[1]"/>
    <xsl:text>',</xsl:text>
    <xsl:value-of select="count(m:rels/m:rel)"/>
    <xsl:text>,</xsl:text>
    <xsl:value-of select="count($changes)"/>
    <xsl:text>,'</xsl:text>
    <xsl:value-of select="substring($changes[last()]/@time, 1, 10)"/>
    <xsl:text>','</xsl:text>
    <xsl:value-of select="substring-after($changes[last()]/@actor, '#')"/>
    <xsl:text>']</xsl:text>
  </xsl:template>
  
  <xsl:template match="doc:*">
  	<xsl:variable name="txt" select="normalize-space(replace(replace(., '''', '\\'''), '\n', ' '))"/>
  	<xsl:value-of select="substring($txt, 0, 64)"/>
  	<xsl:if test="string-length($txt) > 65">...</xsl:if>
  </xsl:template>
  
</xsl:stylesheet>
